package com.sojson.util.idcard.tysj;

import java.io.IOException;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.config.exception.TransException;
import com.sojson.result.bean.Status;
import com.sojson.util.DateUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.SystemParamBaseUtil;
import com.sojson.util.encryption.Md5Util;
import com.sojson.util.httpclient.HttpClientUtil;
import com.sojson.util.idcard.IdCardUtilInterface;
import com.sojson.util.idcard.tysj.bean.TysjConfig;
import com.sojson.util.idcard.tysj.bean.TysjParam;
import com.sojson.util.idcard.tysj.bean.TysjResponse;
import com.sojson.util.idcard.tysj.bean.child.response.TysjResData;
import com.sojson.util.idcard.tysj.constant.TysjConstant;
import com.sojson.util.idcard.tysj.enums.ETysjApiType;
import com.sojson.util.json.JsonUtil;

/**
 * 天眼数据身份证工具类
 * 
 * @author liu
 * @date 2021-07-29
 */
public class IdCardTysjUtil implements IdCardUtilInterface {

    private static IdCardTysjUtil idCardUtil;

    /**
     * 验证姓名和身份证号是否匹配
     * 
     * @param name
     * @param idCard
     * @return
     * @throws Exception 
     */
    @Override
    public Status nameAndIdCard(String name, String idCard) throws Exception {
        // 获取配置参数
        TysjConfig config = getConfig();

        TysjParam param = new TysjParam();
        param.setAppid(config.getAppId());
        param.setTimestamp(DateUtil.nowLong());
        param.setName(name);
        param.setIdcard(idCard);
        param.setSign(getSign(param, config.getAppSecurity()));

        String doGetByParam = HttpClientUtil.doGetByParam(ETysjApiType.NAME_AND_ID_CARD.getType(), objectToJson(param));
        TysjResponse jsonToObject = jsonToObject(doGetByParam);
        Status cusStatus = jsonToObject.getCusStatus();
        if (cusStatus.isFail()) {
            throw new TransException(cusStatus.getMsg());
        }

        TysjResData data = jsonToObject.getData();
        if (data.getResult() == TysjConstant.NAME_AND_ID_CARD_SUCCESS) {
            cusStatus.setCode(TransErrorCode.SUCCESS.getCode());
        } else if (data.getResult() == TysjConstant.NAME_AND_ID_CARD_FAIL) {
            cusStatus.setCode(TransErrorCode.FAIL.getCode());
            cusStatus.setMsg(data.getDesc());
        } else if (data.getResult() == TysjConstant.NAME_AND_ID_CARD_NO) {
            cusStatus.setCode(TransErrorCode.FAIL.getCode());
            cusStatus.setMsg(data.getDesc());
        }
        return cusStatus;
    }

    /**
     * 获取签名
     * 
     * @param param
     * @param appSecurity
     * @return
     * @throws IOException
     */
    private String getSign(TysjParam param, String appSecurity) throws IOException {
        String c = "&";
        String signParam = new StringBuilder(param.getAppid()).append(c).append(param.getTimestamp()).append(c)
            .append(appSecurity).toString();
        return Md5Util.encryption(signParam);
    }

    /**
     * 获取Appid
     * 
     * @return
     * @throws IOException 
     */
    private String getAppid() throws IOException {
        return get(TysjConstant.APPID);
    }

    /**
     * 获取AppSecurity
     * 
     * @return
     * @throws IOException 
     */
    private String getAppSecurity() throws IOException {
        return get(TysjConstant.APP_SECURITY);
    }

    /**
     * 获取天眼数据配置参数
     * 
     * @return
     * @throws IOException 
     */
    private TysjConfig getConfig() throws IOException {
        TysjConfig config = new TysjConfig();
        config.setAppId(getAppid());
        config.setAppSecurity(getAppSecurity());
        return config;
    }

    /**
     * 判断一个对象是否是空(空对象)
     * 
     * @param obj
     * @return
     */
    private static boolean isBlankObject(Object obj) {
        return StringUtil.isBlankObject(obj);
    }

    /**
     * 将对象转换成json字符串(值为空的不解析)
     * 
     * @param data 被转换的对象
     * @return 得到的字符串
     */
    private String objectToJson(Object data) throws IOException {
        return JsonUtil.objectToJson(data);
    }

    /**
     * 将json字符串转换成对象
     * 
     * @param jsonData json字符串
     * @return 得到的对象
     */
    private TysjResponse jsonToObject(String jsonData) {
        TysjResponse jsonToObject = JsonUtil.jsonToObject(jsonData, TysjResponse.class);

        Status status = new Status();
        jsonToObject.setCusStatus(status);
        if (jsonToObject.getSuccess()) {
            status.setCode(TransErrorCode.SUCCESS.getCode());
        } else {
            status.setCode(TransErrorCode.FAIL.getCode());
            status.setMsg(jsonToObject.getMsg());
        }

        return jsonToObject;
    }

    /**
     * 从缓存中获取值
     * 
     * @param key
     * @return
     * @throws IOException
     */
    private String get(String key) throws IOException {
        return SystemParamBaseUtil.get(key);
    }

    public static IdCardTysjUtil getInstances() {
        if (isBlankObject(idCardUtil)) {
            idCardUtil = new IdCardTysjUtil();
        }
        return idCardUtil;
    }

}